<?php

declare(strict_types=1);

namespace App\Payment\Model;

use Hyperf\Database\Model\Events\Created;
use Hyperf\Database\Model\Events\Updated;
use Mine\MineModel;

/**
 * @property int $id
 * @property int $method_id 支付方式ID
 * @property int $diamond 钻石数量
 * @property int $money 价格(分)
 * @property int $ios_product_id 苹果内购产品ID
 * @property float $sort 排序
 * @property int $recharge_id 首充送充
 * @property int $is_first 是否首充(1是，2否)
 * @property int $status 状态1可用0禁用
 * @property int $created_by 创建人
 * @property int $create_time 创建时间
 * @property int $update_time 更新时间
 */
class PaymentMember extends MineModel
{
    protected ?string $dateFormat = 'U';
    const CREATED_AT = 'create_time';
    const UPDATED_AT = 'update_time';
    /**
     * The table associated with the model.
     */
    protected ?string $table = 'payment_member';

    /**
     * The attributes that are mass assignable.
     */
    protected array $fillable = ['id', 'method_id', 'diamond', 'money', 'ios_product_id', 'sort', 'recharge_id', 'is_first', 'status', 'created_by', 'create_time', 'update_time'];

    /**
     * The attributes that should be cast to native types.
     */
    protected array $casts = ['id' => 'integer', 'method_id' => 'integer', 'diamond' => 'integer', 'money' => 'integer', 'ios_product_id' => 'integer', 'sort' => 'float', 'recharge_id' => 'integer', 'is_first' => 'integer', 'status' => 'integer', 'created_by' => 'integer', 'create_time' => 'integer', 'update_time' => 'integer'];

    public function setDiamondAttribute($value)
    {
        $this->attributes['diamond'] = (int)($value * 100);
    }

    public function getMoneyAttribute($value)
    {
        return number_format($value / 100, 2, '.', '');
    }


    public function setMoneyAttribute($value)
    {
        $this->attributes['money'] = (int)($value * 100);
    }

    public function getDiamondAttribute($value)
    {
        return number_format($value / 100, 2, '.', '');
    }


    public function created(Created $event)
    {
        echo '-----'.$this->table.':新增后---->>' . PHP_EOL;
        $redisKeys = makeRedisTableKeys('payment_method', 'list');
        $keys=\App\Lib\Library\RedisOptions::getRedisInstance()->keys($redisKeys);
        \App\Lib\Library\RedisOptions::del($keys);
    }

    public function updated(Updated $event)
    {
        echo '-----'.$this->table.':更新后---->>' . PHP_EOL;
        $redisKeys = makeRedisTableKeys('payment_method', 'list');
        $keys=\App\Lib\Library\RedisOptions::getRedisInstance()->keys($redisKeys);
        \App\Lib\Library\RedisOptions::del($keys);
    }
}
